package com.chart.controller;

import com.chart.model.StudentEntity;
import com.chart.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {

	@Resource
	private StudentService studentService;
	/**
	 *  查询数据
	 * @return
	 */
	@RequestMapping("getAll.controller")
    @ResponseBody
	public List<StudentEntity> getStudentEntityList() {
        List<StudentEntity> all = studentService.getAll();

        return all;
	}

	/**
	 *  查询数据
	 * @return
	 */
	@RequestMapping("getListByCondition.controller")
    @ResponseBody
	public List<StudentEntity> getListByCondition(StudentEntity student,Integer startScore,Integer endScore,Integer startTotalScore,Integer endTotalScore) {

        List<StudentEntity> all = studentService.getListByCondition(student,startScore,endScore,startTotalScore,endTotalScore);

        return all;
	}
	/**
	 *  删除
	 * @return
	 */
	@RequestMapping("delete.controller")
	public void deleteStudentEntity(StudentEntity studentEntity, HttpServletResponse response) {
		try {
			if(studentEntity.getId()!=null){
				studentService.deleteStudent(studentEntity);
				response.getWriter().write("success");
			}else {
                response.getWriter().write("n");
			}
		}catch (Exception e){
		    try {
                response.getWriter().write("fail");
            }catch (Exception e1){

            }
		}


	}
	/**
	 *  修改
	 * @return
	 */
	@RequestMapping("update.controller")
	public void  updateStudent(StudentEntity studentEntity,HttpServletResponse response) {
		try {
            if(studentEntity.getId()!=null) {
                studentService.updateStudent(studentEntity);
                response.getWriter().write("success");
            }else {
                response.getWriter().write("n");
            }
		}catch (Exception e){
            try {
                response.getWriter().write("fail");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
	}

	/**
	 *  新增
	 * @return
	 */
	@RequestMapping("add.controller")
	@ResponseBody
	public Map<String,String> addStudentEntity(StudentEntity studentEntity) {
		Map<String,String> map=new HashMap<>();
		try {
			studentService.add(studentEntity);
			map.put("message","success");
		}catch (Exception e){
			map.put("message","fail");
		}

		return map;
	}

}